package com.cashmama.market.api.domain.service.impl;

import com.cashmama.market.api.domain.service.ApiProductService;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-23 14:34
 **/
@Service
public class ProductIntegrationServiceImpl implements ApiProductService {

    @Autowired
    private ProductFacade productFacade;

    @Override
    public List<ProductCategory> getActiveProductCategoryList(String appCode) {
        Response<List<ProductCategory>> productCategoryList = productFacade.getProductCategoryList(appCode);
        if (CollectionUtils.isEmpty(productCategoryList.getData())) {
            return Lists.newArrayList();
        }

        List<ProductCategory> activeCategoryList = Lists.newArrayList();
        for (ProductCategory productCategory : productCategoryList.getData()) {
            if (productCategory.getIsSelling() == 1) {
                activeCategoryList.add(productCategory);
            }
        }
        return activeCategoryList;
    }

    @Override
    public List<ProductCategory> getAllActiveProductCategoryList() {
        Response<List<ProductCategory>> response = productFacade.getAllProductCategory();
        if(!response.isSuccess()){
            throw new ApplicationException(response.getMsg());
        }
        return response.getData();
    }

    @Override
    public ProductCategory getProductCategoryByMerchantCode(String merchantCode) {
        Response<List<ProductCategory>> response = productFacade.getProductCategoriesByMerchantCode(merchantCode);
        if(!response.isSuccess()){
            throw new ApplicationException(response.getMsg());
        }
        if(CollectionUtils.isNotEmpty(response.getData())){
            return response.getData().get(0);
        }
        return null;
    }
}
